<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      height="480"
      row-key="permissionId"
      border
    >
      <el-table-column prop="userId" label="用户Id" sortable> </el-table-column>
      <el-table-column prop="userName" label="用户昵称" sortable> </el-table-column>
      <el-table-column prop="gender" label="用户性别" sortable> </el-table-column>
      <el-table-column prop="phone" label="手机号" sortable> </el-table-column>
      <!-- <el-table-column prop="balance" label="用户余额" sortable> </el-table-column>
      <el-table-column prop="expend" label="总消费" sortable> </el-table-column>
       -->
      <!-- <el-table-column prop="userPhoto" label="照片" sortable> 
        <template slot-scope="scope">
          <img :src="'http://localhost:8080/images/'+scope.row.photo" width="100" height="75"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="info" @click="updateUI(scope.row)" plain size="mini">修改 </el-button>
          <el-button type="danger" @click="del(scope.row)" plain size="mini">删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户Id" :label-width="formLabelWidth">
          <el-input v-model="form.userId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" :label-width="formLabelWidth">
          <el-input v-model="form.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="照片" :label-width="formLabelWidth">


          <el-upload
            class="upload-demo"
            action="http://localhost:8080/user/upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
            :on-success="uploadSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件,且不超过500kb
            </div>
          </el-upload>
          <img v-if="form.photo" :src="'http://localhost:8080/images/'+ form.photo" width="200" height="150">

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageInfo.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    ></el-pagination>
  </div>
</template>

<script>
import { findAll , doSaveOrUpdate,deleteByUserId } from "@/api/user/index.js";

export default {
  mounted() {
    this.updateTable();
  },
  data() {
    return {
      tableData: [],
      fileList:[],
      form:{},
      dialogFormVisible: false,
      formLabelWidth:"180px",
       pageInfo: {
        total: 0,
        page: 1,
        size: 5
      },
    };
  },
  methods: {
    handleSizeChange(size){
      this.pageInfo.size = size;
      this.updateTable();
    },
    handleCurrentChange(page){
      this.pageInfo.page = page;
      this.updateTable();
    },
    updateTable(){
      findAll(this.pageInfo).then((r) => {
      this.tableData = r.data.list;

      this.pageInfo.total = r.data.total;

    });
    },
    handlePreview(){

    },
    handleRemove(){

    },
    del(row) {
      let userId = row.userId;
      deleteByUserId(userId).then(r=>{
        if(r.code == 200){
          this.updateTable();
          this.$message.success(r.msg);
          return;
        }
        this.$message.error(r.msg);
      })
    },
    updateUI(row) {
      this.form={};
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    saveOrUpdate(){
      doSaveOrUpdate(this.form).then(r=>{
        if(r.code == 200){
          this.dialogFormVisible = false;
          this.$message.success(r.msg);
          this.updateTable();
          this.fileList=[]
        }
      })
    },
    uploadSuccess(r){
      this.form.photo = r.data.newFileName;
    },
    distRoleToUser(row){
      this.$router.push(`DistRoleToUser/${row.userId}`);
    }
  },
};
</script>

<style>

</style>